﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _49RoutedCommand
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InputBinding ib = new InputBinding(ApplicationCommands.Cut, new KeyGesture(Key.D, ModifierKeys.Control));

            textBox1.InputBindings.Add(ib);
            CommandBinding cb = new CommandBinding(ApplicationCommands.Cut);
            cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);
            cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecuted);
            textBox1.CommandBindings.Add(cb);
        }

        void cb_Executed (object sender, ExecutedRoutedEventArgs e)
        {
            TextBox textbox = sender as TextBox;
            MessageBox.Show(textbox.SelectedText);
            Clipboard.SetText(textbox.SelectedText);
            textbox.SelectedText = string.Empty;
        }
        void cb_CanExecuted(object sender, CanExecuteRoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            e.CanExecute = !String.IsNullOrEmpty(textBox.SelectedText);
        }
    }
}
